package com.example.findmystuff;

import com.database.findmystuff.Item;
import com.database.findmystuff.User;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import java.util.LinkedList;
import java.util.Set;
import android.widget.Button;
import android.widget.TextView;

public class ListItemsActivity extends Activity {

	TextView items;
	Button homeButton;
	public Set<User> userList;
	public LinkedList<Item> itemList;
	public int filter;
	public String search, location;
	/**
	 * This method is called when the activity is created
	 * @param savedInstanceState a bundle used to save the state that this instance of the activity is in.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_items);
		
		filter=0;
		search="";
		location="";
		items = (TextView) findViewById(R.id.item1Text);
		homeButton = (Button) findViewById(R.id.homeButton);
		userList=MainActivity.database.getUserList();
		items.setText("");
		
		Bundle extras=getIntent().getExtras();
		if(extras!=null){
			filter=extras.getInt("filter");
			search=extras.getString("search");
		}
		
		switch(filter){
		case 0:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
					}
				}
			}
			break;
		case 1:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						if(itemList.get(i).getCategory().contains(search)){
							items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
						}
					}
				}
			}
			break;
		case 2:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						if(itemList.get(i).getDate().contains(search)){
							items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
						}
					}
				}
			}
			break;
		case 3:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						if(itemList.get(i).getStatus().contains(search)){
							items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
						}
					}
				}
			}
			break;
		case 4:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						if(itemList.get(i).getName().contains(search)){
							if(itemList.get(i).getLocation().contains(location)){
								items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
							}
						}
					}
				}
			}
			break;
		case 5:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						if(itemList.get(i).getName().contains(search)){
							items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
						}
					}
				}
			}
			break;
		default:
			for(User u:userList){
				if(MainActivity.database.getItemList(u)!=null){
					itemList=MainActivity.database.getItemList(u);
					for(int i=0;i<itemList.size();i++){
						items.setText(items.getText()+"\n\n"+itemList.get(i).toString());
					}
				}
			}
			break;
		}
		
		homeButton.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(MainActivity.loggedUser.equals("admin")){
							Intent i = new Intent(ListItemsActivity.this, AdminHomeActivity.class);
							startActivity(i);
						}
						else{
							Intent i = new Intent(ListItemsActivity.this, HomeActivity.class);
							startActivity(i);
						}
					}
				});
	}
	/**
	 * Initializes the contents of the Activity's standard options menu.
	 * @param menu the standard options menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_items, menu);
		return true;
	}

}
